home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
xlib
/
pixtest.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
35KB
|
1,319 lines
/*
* (c) Copyright 1994, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
#include <GL/glx.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <X11/keysym.h>
#include <malloc.h>
static int RGB_SB_attributes[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
static int RGB_DB_attributes[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DOUBLEBUFFER,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
static int CI_SB_attributes[] = {
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
static int CI_DB_attributes[] = {
GLX_DOUBLEBUFFER,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None,
};
Display *dpy;
Window window;
GLfloat *fltImage; /* GL_RGBA, GL_FLOAT */
GLubyte *bitmapImage;
GLint imageWidth, imageHeight;
GLfloat zoomx, zoomy;
void *newimage;
GLenum type, format, copyFormat;
GLint centerx, centery;
GLint scissor;
GLint biasing;
GLint mapping;
GLint test;
GLint rgbMode;
GLint alphatst;
GLint swapBytes;
GLfloat quant4[4];
void *tempbuf;
GLint windowWidth, windowHeight;
GLint alignShift;
GLboolean dlisted;
GLboolean doubleBuffer;
GLuint dlistnum;
GLuint maxindex;
#define TEST_DRAW 0
#define TEST_READ 1
#define TEST_COPY 2
typedef struct _rawImageRec {
unsigned short imagic;
unsigned short type;
unsigned short dim;
unsigned short sizeX, sizeY, sizeZ;
unsigned long min, max;
unsigned long wasteBytes;
char name[80];
unsigned long colorMap;
FILE *file;
unsigned char *tmp, *tmpR, *tmpG, *tmpB;
unsigned long rleEnd;
unsigned long *rowStart;
long *rowSize;
} rawImageRec;
typedef struct image {
long sizeX, sizeY;
unsigned char *data;
} Image;
Image *image;
static rawImageRec *RawImageOpen(char *fileName)
{
rawImageRec *raw;
long x;
raw = (rawImageRec *)malloc(sizeof(rawImageRec));
if (raw == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
if ((raw->file = fopen(fileName, "rb")) == NULL) {
perror(fileName);
exit(1);
}
fread(raw, 1, 12, raw->file );
raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
raw->tmpB == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
if ((raw->type & 0xFF00) == 0x0100) {
x = raw->sizeY * raw->sizeZ * sizeof(long);
raw->rowStart = (unsigned long *)malloc(x);
raw->rowSize = (long *)malloc(x);
if (raw->rowStart == NULL || raw->rowSize == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
raw->rleEnd = 512 + (2 * x);
fseek(raw->file, 512L, SEEK_SET);
fread(raw->rowStart, 1, x, raw->file);
fread(raw->rowSize, 1, x, raw->file);
}
return raw;
}
static void RawImageClose(rawImageRec *raw)
{
fclose(raw->file);
free(raw->tmp);
free(raw->tmpR);
free(raw->tmpG);
free(raw->tmpB);
free(raw);
}
static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, long y, long z)
{
unsigned char *iPtr, *oPtr, pixel;
long count;
if ((raw->type & 0xFF00) == 0x0100) {
fseek(raw->file, raw->rowStart[y+z*raw->sizeY], SEEK_SET);
fread(raw->tmp, 1, raw->rowSize[y+z*raw->sizeY], raw->file);
iPtr = raw->tmp;
oPtr = buf;
while (1) {
pixel = *iPtr++;
count = (long)(pixel & 0x7f);
if (!count)
return;
if (pixel & 0x80) {
while (count--) {
*oPtr++ = *iPtr++;
}
} else {
pixel = *iPtr++;
while (count--) {
*oPtr++ = pixel;
}
}
}
} else {
fseek(raw->file, 512L+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
SEEK_SET);
fread(buf, 1, raw->sizeX, raw->file);
}
}
static void RawImageGetData(rawImageRec *raw, Image *final)
{
long i, j;
unsigned char *ptr;
final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
if (final->data == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
ptr = final->data;
for (i=0; i<raw->sizeY; i++) {
RawImageGetRow(raw, raw->tmpR, i, 0);
RawImageGetRow(raw, raw->tmpG, i, 1);
RawImageGetRow(raw, raw->tmpB, i, 2);
for (j=0; j<raw->sizeX; j++) {
*ptr++ = *(raw->tmpR + j);
*ptr++ = *(raw->tmpG + j);
*ptr++ = *(raw->tmpB + j);
}
}
}
Image *ImageLoad(char *fileName)
{
rawImageRec *raw;
Image *final;
raw = RawImageOpen(fileName);
final = (Image *)malloc(sizeof(Image));
if (final == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
final->sizeX = raw->sizeX;
final->sizeY = raw->sizeY;
RawImageGetData(raw, final);
RawImageClose(raw);
return final;
}
void createFloatImage(void)
{
GLubyte *rawData;
GLint x,y;
GLfloat *finalImage;
GLfloat alpha;
rawData = image->data;
fltImage = malloc(imageWidth * imageHeight * sizeof(GLfloat) * 4);
tempbuf = malloc(imageWidth * imageHeight * sizeof(GLdouble) * 4 + 3);
finalImage = fltImage;
for (y = 0; y < imageHeight; y++) {
for (x = 0; x < imageWidth; x++) {
*finalImage++ = *rawData++ / 255.0;
alpha = *finalImage++ = *rawData++ / 255.0;
*finalImage++ = *rawData++ / 255.0;
*finalImage++ = alpha;
}
}
}
static void Init(void)
{
int i;
GLfloat array[8];
imageWidth = image->sizeX;
imageHeight = image->sizeY;
centerx = image->sizeX;
centery = image->sizeY;
createFloatImage();
newimage = image->data;
for (i=0; i<4; i++) {
quant4[i] = (2*i+1)/8.0;
}
glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 4, quant4);
glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 4, quant4);
glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 4, quant4);
glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 4, quant4);
for (i=0; i<8; i++) {
array[i] = (i&1) ? 1 : 0;
}
glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 8, array);
for (i=0; i<8; i++) {
array[i] = (i&2) ? 1 : 0;
}
glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 8, array);
for (i=0; i<8; i++) {
array[i] = (i&4) ? 1 : 0;
}
glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 8, array);
array[0] = 0;
array[1] = 1;
glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 2, array);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_PACK_LSB_FIRST, GL_TRUE);
glScissor(0,0,windowWidth,windowHeight);
glEnable(GL_SCISSOR_TEST);
glColor3f(1.0, 1.0, 1.0);
dlistnum = glGenLists(1);
}
static void Redraw(void)
{
GLubyte *s;
int i,j;
static int lasttest = TEST_DRAW;
glViewport(0, 0, 4*imageWidth, 2*imageHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 4*imageWidth, 0, 2*imageHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);
glScissor(0,0,windowWidth,windowHeight);
glClearColor(0.0, 0.0, 0.0, 0.0);
if (alphatst && (type == GL_BITMAP ||
format == GL_ALPHA || format == GL_LUMINANCE_ALPHA)) {
glClearColor(0.3, 0.3, 0.3, 0.0);
}
glClear(GL_COLOR_BUFFER_BIT);
if (test != lasttest) {
lasttest = test;
switch(test) {
case TEST_DRAW:
printf("Test = DrawPixels\n");
break;
case TEST_COPY:
printf("Test = CopyPixels\n");
break;
case TEST_READ:
printf("Test = ReadPixels\n");
break;
}
}
if (format == GL_DEPTH_COMPONENT) {
glDepthFunc(GL_ALWAYS);
glClearDepth(0.5);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
}
if (format == GL_STENCIL_INDEX) {
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 255);
}
glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
if (test == TEST_READ || test == TEST_COPY) {
glRasterPos3f(0,0,-1);
glBitmap(0,0,0,0,centerx - imageWidth/2.0,
centery - imageHeight/2.0,NULL);
glDrawPixels(imageWidth, imageHeight, format, type,
newimage);
}
glPixelZoom(zoomx, zoomy);
if (format == GL_DEPTH_COMPONENT) {
glDepthFunc(GL_GEQUAL);
}
if (biasing) {
glPixelTransferf(GL_RED_SCALE, -1);
glPixelTransferf(GL_RED_BIAS, 1);
glPixelTransferf(GL_GREEN_SCALE, -1);
glPixelTransferf(GL_GREEN_BIAS, 1);
glPixelTransferf(GL_BLUE_SCALE, -1);
glPixelTransferf(GL_BLUE_BIAS, 1);
}
if (mapping) {
glPixelTransferi(GL_MAP_COLOR, GL_TRUE);
}
if (scissor) {
glScissor(imageWidth/2, imageHeight/2,
imageWidth, imageHeight);
}
if (alphatst) {
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5);
}
if (test == TEST_DRAW) {
if (dlisted) {
glNewList(dlistnum, GL_COMPILE);
}
glRasterPos3f(0,0,-1);
glBitmap(0,0,0,0,centerx - zoomx * imageWidth / 2,
centery - zoomy * imageHeight / 2,NULL);
glDrawPixels(imageWidth, imageHeight, format, type,
newimage);
if (dlisted) {
glEndList();
glCallList(dlistnum);
}
} else if (test == TEST_READ) {
glReadPixels(centerx - (1+imageWidth)/2,
centery - (1+imageHeight)/2, imageWidth,
imageHeight, format, type, ((GLubyte *) tempbuf) + alignShift);
} else if (test == TEST_COPY) {
glRasterPos3f(0,0,-1);
glBitmap(0,0,0,0,imageWidth - zoomx * imageWidth / 2,
imageHeight - zoomy * imageHeight / 2,NULL);
glCopyPixels(centerx - (1+imageWidth) / 2,
centery - (1+imageHeight) / 2, imageWidth,
imageHeight, copyFormat);
}
if (biasing) {
glPixelTransferf(GL_RED_SCALE, 1);
glPixelTransferf(GL_RED_BIAS, 0);
glPixelTransferf(GL_GREEN_SCALE, 1);
glPixelTransferf(GL_GREEN_BIAS, 0);
glPixelTransferf(GL_BLUE_SCALE, 1);
glPixelTransferf(GL_BLUE_BIAS, 0);
}
if (mapping) {
glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
}
if (test == TEST_READ) {
glRasterPos3f(0,0,-1);
glBitmap(0,0,0,0,imageWidth - zoomx * imageWidth / 2,
imageHeight - zoomy * imageHeight / 2,NULL);
glDrawPixels(imageWidth, imageHeight, format, type,
((GLubyte *) tempbuf) + alignShift);
}
if (format == GL_STENCIL_INDEX) {
if (type == GL_BITMAP) {
glStencilFunc(GL_EQUAL, 1, 1);
} else if (type == GL_BYTE) {
glStencilFunc(GL_LEQUAL, (maxindex/4)+1, maxindex);
} else {
glStencilFunc(GL_LEQUAL, (maxindex/2)+1, maxindex);
}
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glRecti(0, 0, 4*imageWidth, 2*imageHeight);
}
glFlush();
if (format == GL_DEPTH_COMPONENT) {
glDisable(GL_DEPTH_TEST);
}
if (format == GL_STENCIL_INDEX) {
glDisable(GL_STENCIL_TEST);
}
if (scissor) {
glScissor(0,0,windowWidth,windowHeight);
}
if (alphatst) {
glDisable(GL_ALPHA_TEST);
}
glPixelZoom(1.0, 1.0);
if (doubleBuffer) {
glXSwapBuffers(dpy, window);
}
}
GLboolean okCombo(void)
{
if (rgbMode && type == GL_BITMAP && format != GL_COLOR_INDEX &&
format != GL_STENCIL_INDEX) {
return GL_FALSE;
}
if (test == TEST_READ && rgbMode && format == GL_COLOR_INDEX) {
return GL_FALSE;
}
return GL_TRUE;
}
void makeNewImage(void)
{
int components;
int bytesPerComp;
GLfloat *myimage;
int i,j,k;
int index;
GLfloat indexscale;
int imageSize;
if (swapBytes) {
if (type == GL_BITMAP) {
printf("MSB_FIRST, ");
} else {
printf("SWAP_BYTES, ");
}
} else {
if (type == GL_BITMAP) {
printf("LSB_FIRST, ");
}
}
if (alignShift) {
printf("(%d*), ", alignShift);
}
switch(type) {
case GL_BYTE:
bytesPerComp = 1;
printf("Type: GL_BYTE, ");
break;
case GL_UNSIGNED_BYTE:
bytesPerComp = 1;
printf("Type: GL_UNSIGNED_BYTE, ");
break;
case GL_INT:
bytesPerComp = 4;
printf("Type: GL_INT, ");
break;
case GL_UNSIGNED_INT:
bytesPerComp = 4;
printf("Type: GL_UNSIGNED_INT, ");
break;
case GL_BITMAP:
printf("Type: GL_BITMAP, ");
break;
case GL_FLOAT:
bytesPerComp = 4;
printf("Type: GL_FLOAT, ");
break;
case GL_UNSIGNED_SHORT:
bytesPerComp = 2;
printf("Type: GL_UNSIGNED_SHORT, ");
break;
case GL_SHORT:
bytesPerComp = 2;
printf("Type: GL_SHORT, ");
break;
}
copyFormat = GL_COLOR;
index = 0;
switch(format) {
case GL_RGB:
components = 3;
printf("Format: GL_RGB\n");
break;
case GL_RED:
components = 1;
printf("Format: GL_RED\n");
break;
case GL_GREEN:
components = 1;
printf("Format: GL_GREEN\n");
break;
case GL_BLUE:
components = 1;
printf("Format: GL_BLUE\n");
break;
case GL_ALPHA:
components = 1;
printf("Format: GL_ALPHA\n");
break;
case GL_LUMINANCE_ALPHA:
components = 2;
printf("Format: GL_LUMINANCE_ALPHA\n");
break;
case GL_LUMINANCE:
components = 1;
printf("Format: GL_LUMINANCE\n");
break;
case GL_RGBA:
components = 4;
printf("Format: GL_RGBA\n");
break;
case GL_COLOR_INDEX:
components = 1;
index = 1;
printf("Format: GL_COLOR_INDEX\n");
break;
case GL_DEPTH_COMPONENT:
components = 1;
copyFormat = GL_DEPTH;
printf("Format: GL_DEPTH_COMPONENT\n");
break;
case GL_STENCIL_INDEX:
components = 1;
index = 1;
copyFormat = GL_STENCIL;
printf("Format: GL_STENCIL_INDEX\n");
break;
}
if (index) {
GLint bufferbits, typebits;
switch (format) {
case GL_COLOR_INDEX:
glGetIntegerv(GL_INDEX_BITS, &bufferbits);
break;
case GL_STENCIL_INDEX:
glGetIntegerv(GL_STENCIL_BITS, &bufferbits);
break;
}
maxindex = (bufferbits > 0) ? ~(~1 << bufferbits-1) : 0;
switch (type) {
case GL_BITMAP:
case GL_FLOAT:
typebits = bufferbits;
break;
case GL_BYTE:
typebits = 8*sizeof(GLbyte) - 1;
break;
case GL_UNSIGNED_BYTE:
typebits = 8*sizeof(GLubyte);
break;
case GL_INT:
typebits = 8*sizeof(GLint) - 1;
break;
case GL_UNSIGNED_INT:
typebits = 8*sizeof(GLuint);
break;
case GL_SHORT:
typebits = 8*sizeof(GLshort) - 1;
break;
case GL_UNSIGNED_SHORT:
typebits = 8*sizeof(GLushort);
break;
}
if (typebits < bufferbits) {
indexscale = (GLfloat) pow(2.0, (double) typebits) - 1.0;
} else {
indexscale = (GLfloat) pow(2.0, (double) bufferbits) - 1.0;
}
} else {
maxindex = 0;
indexscale = 0;
}
/* Build the new image */
free((void *) ((int) (newimage) & 0xfffffffc));
if (type != GL_BITMAP) {
imageSize = imageWidth*imageHeight*components*bytesPerComp;
} else {
imageSize = ((imageWidth + 7)/8)*imageHeight;
}
newimage = malloc(imageSize + 3);
myimage = fltImage;
if (format == GL_GREEN) myimage += 1;
else if (format == GL_BLUE || format == GL_LUMINANCE ||
format == GL_LUMINANCE_ALPHA) myimage += 2;
else if (format == GL_ALPHA) myimage += 3;
if (type == GL_BITMAP) {
GLubyte *data;
GLint bit;
GLubyte byte;
data = newimage;
for (i=0; i<imageHeight; i++) {
bit = 0;
byte = 0;
for (j=0; j<imageWidth; j++) {
if (*myimage++ > 0.5) {
if (swapBytes) {
byte |= 1<<(7-bit);
} else {
byte |= 1<<bit;
}
}
bit++;
if (bit == 8) {
bit = 0;
*data++ = byte;
byte = 0;
}
myimage += 3;
}
if (bit) {
*data++ = byte;
}
}
} else if (type == GL_UNSIGNED_BYTE) {
GLubyte *data;
data = newimage;
for (i=0; i<imageHeight; i++) {
for (j=0; j<imageWidth; j++) {
for (k=0; k<components; k++) {
if (index) {
*data++ = *myimage++ * indexscale;
} else {
*data++ = *myimage++ * 255.0;
}
}
myimage+=(4-components);
}
}
} else if (type == GL_BYTE) {
GLbyte *data;
data = newimage;
for (i=0; i<imageHeight; i++) {
for (j=0; j<imageWidth; j++) {
for (k=0; k<components; k++) {
if (index) {
*data++ = *myimage++ * indexscale;
} else {
*data++ = *myimage++ * 127.0;
}
}
myimage+=(4-components);
}
}
} else if (type == GL_UNSIGNED_SHORT) {
GLushort *data;
GLubyte *temp1, *temp2;
GLushort answer;
data = newimage;
for (i=0; i<imageHeight; i++) {
for (j=0; j<imageWidth; j++) {
for (k=0; k<components; k++) {
if (index) {
answer = *myimage++ * indexscale;
} else {
answer = *myimage++ * (GLfloat) 65535.0;
}
if (swapBytes) {
temp2 = (GLubyte *) &answer;
temp1 = (GLubyte *) data;
temp1[0] = temp2[1];
temp1[1] = temp2[0];
} else {
data[0] = answer;
}
data++;
}
myimage+=(4-components);
}
}
} else if (type == GL_SHORT) {
GLshort *data;
GLubyte *temp1, *temp2;
GLshort answer;
data = newimage;
for (i=0; i<imageHeight; i++) {
for (j=0; j<imageWidth; j++) {
for (k=0; k<components; k++) {
if (index) {
answer = *myimage++ * indexscale;
} else {
answer = *myimage++ * (GLfloat) 32767.0;
}
if (swapBytes) {
temp2 = (GLubyte *) &answer;
temp1 = (GLubyte *) data;
temp1[0] = temp2[1];
temp1[1] = temp2[0];
} else {
data[0] = answer;
}
data++;
}
myimage+=(4-components);
}
}
} else if (type == GL_INT) {
GLint *data;
GLubyte *temp1, *temp2;
GLint answer;
data = newimage;
for (i=0; i<imageHeight; i++) {
for (j=0; j<imageWidth; j++) {
for (k=0; k<components; k++) {
if (index) {
answer = *myimage++ * indexscale;
} else {
answer = *myimage++ * (GLfloat) 2147482500.0;
}
if (swapBytes) {
temp2 = (GLubyte *) &answer;
temp1 = (GLubyte *) data;
temp1[0] = temp2[3];
temp1[1] = temp2[2];
temp1[2] = temp2[1];
temp1[3] = temp2[0];
} else {
data[0] = answer;
}
data++;
}
myimage+=(4-components);
}
}
} else if (type == GL_UNSIGNED_INT) {
GLuint *data;
GLubyte *temp1, *temp2;
GLuint answer;
data = newimage;
for (i=0; i<imageHeight; i++) {
for (j=0; j<imageWidth; j++) {
for (k=0; k<components; k++) {
if (index) {
answer = *myimage++ * indexscale;
} else {
answer = *myimage++ * (GLfloat) 4294965000.0;
}
if (swapBytes) {
temp2 = (GLubyte *) &answer;
temp1 = (GLubyte *) data;
temp1[0] = temp2[3];
temp1[1] = temp2[2];
temp1[2] = temp2[1];
temp1[3] = temp2[0];
} else {
data[0] = answer;
}
data++;
}
myimage+=(4-components);
}
}
} else if (type == GL_FLOAT) {
GLfloat *data;
GLubyte *temp1, *temp2;
GLfloat answer;
data = newimage;
for (i=0; i<imageHeight; i++) {
for (j=0; j<imageWidth; j++) {
for (k=0; k<components; k++) {
if (index) {
answer = *myimage++ * indexscale;
} else {
answer = *myimage++;
}
if (swapBytes) {
temp2 = (GLubyte *) &answer;
temp1 = (GLubyte *) data;
temp1[0] = temp2[3];
temp1[1] = temp2[2];
temp1[2] = temp2[1];
temp1[3] = temp2[0];
} else {
data[0] = answer;
}
data++;
}
myimage+=(4-components);
}
}
}
if (alignShift) {
memcpy((void *) (((int) newimage)+alignShift), newimage, imageSize);
newimage = (void *) (((int) newimage)+alignShift);
}
}
void changeType(void)
{
do {
switch(type) {
case GL_UNSIGNED_BYTE:
type = GL_BYTE;
break;
case GL_BYTE:
type = GL_UNSIGNED_SHORT;
break;
case GL_UNSIGNED_SHORT:
type = GL_SHORT;
break;
case GL_SHORT:
type = GL_UNSIGNED_INT;
break;
case GL_UNSIGNED_INT:
type = GL_INT;
break;
case GL_INT:
type = GL_FLOAT;
break;
case GL_FLOAT:
type = GL_BITMAP;
break;
case GL_BITMAP:
type = GL_UNSIGNED_BYTE;
break;
}
} while (!okCombo());
makeNewImage();
}
void changeFormat(void)
{
do {
switch(format) {
case GL_RGB:
format = GL_RGBA;
break;
case GL_RGBA:
format = GL_DEPTH_COMPONENT;
break;
case GL_DEPTH_COMPONENT:
format = GL_COLOR_INDEX;
break;
case GL_COLOR_INDEX:
format = GL_STENCIL_INDEX;
break;
case GL_STENCIL_INDEX:
format = GL_RED;
break;
case GL_RED:
format = GL_GREEN;
break;
case GL_GREEN:
format = GL_BLUE;
break;
case GL_BLUE:
format = GL_ALPHA;
break;
case GL_ALPHA:
format = GL_LUMINANCE;
break;
case GL_LUMINANCE:
format = GL_LUMINANCE_ALPHA;
break;
case GL_LUMINANCE_ALPHA:
format = GL_RGB;
break;
}
} while (!okCombo());
makeNewImage();
}
void changeTest(void)
{
do {
switch(test) {
case TEST_DRAW:
test = TEST_COPY;
break;
case TEST_COPY:
test = TEST_READ;
break;
case TEST_READ:
test = TEST_DRAW;
break;
}
} while (!okCombo());
}
static void Usage(void)
{
fprintf(stderr, "Usage: pixtest [-s] [-geometry WxH+X+Y] <filename>\n");
exit(1);
}
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
int main(int argc, char** argv)
{
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
XSizeHints sizehints;
GLXContext cx;
XEvent event;
GLboolean needDisplay;
XColor white;
char *geometry = NULL;
char *filename;
int i;
zoomx = 1.0;
zoomy = 1.0;
format = GL_RGB;
copyFormat = GL_COLOR;
type = GL_UNSIGNED_BYTE;
filename = NULL;
doubleBuffer = GL_TRUE;
rgbMode = GL_TRUE;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-geometry")) {
i++;
geometry = argv[i];
if (geometry == NULL) {
Usage();
}
} else if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 's':
doubleBuffer = GL_FALSE;
break;
case 'c':
rgbMode = GL_FALSE;
break;
default:
Usage();
}
} else {
if (filename == NULL) {
filename=argv[i];
} else {
Usage();
}
}
}
if (filename == NULL) {
Usage();
}
image = ImageLoad(filename);
windowWidth = 4 * image->sizeX;
windowHeight = 2 * image->sizeY;
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
return -1;
}
vi = glXChooseVisual(dpy, DefaultScreen(dpy),
doubleBuffer ? (rgbMode ? RGB_DB_attributes : CI_DB_attributes) :
(rgbMode ? RGB_SB_attributes : CI_SB_attributes));
if (!vi) {
fprintf(stderr, "No appropriate visual on \"%s\"\n",
getenv("DISPLAY"));
return -1;
}
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
rgbMode ? AllocNone : AllocAll);
if (rgbMode) {
white.red = ~0;
white.green = ~0;
white.blue = ~0;
XAllocColor(dpy, cmap, &white);
swa.background_pixel = white.pixel;
} else {
swa.background_pixel = 15;
}
if (!rgbMode) {
XColor buf;
int i;
buf.flags = DoRed | DoGreen | DoBlue;
/* Init color map */
for (i=0; i<16; i++) {
buf.pixel = i;
buf.blue = (i & 4) ? 65535 : 0;
buf.green = (i & 2) ? 65535 : 0;
buf.red = (i & 1) ? 65535 : 0;
if (i > 8) {
buf.red /= 2;
buf.green /= 2;
buf.blue /= 2;
}
XStoreColor(dpy, cmap, &buf);
}
}
sizehints.flags = PPosition | PSize;
sizehints.width = windowWidth;
sizehints.height = windowHeight;
sizehints.x = 10;
sizehints.y = 10;
if(geometry) {
int flags, x, y, width, height;
flags = XParseGeometry(geometry, &x, &y,
(unsigned int *)&width,
(unsigned int *)&height);
if(WidthValue & flags) {
sizehints.flags |= USSize;
sizehints.width = width;
windowWidth = width;
}
if(HeightValue & flags) {
sizehints.flags |= USSize;
sizehints.height = height;
windowHeight = height;
}
if(XValue & flags) {
if(XNegative & flags)
x = DisplayWidth(dpy, DefaultScreen(dpy)) + x
- sizehints.width;
sizehints.flags |= USPosition;
sizehints.x = x;
}
if(YValue & flags) {
if(YNegative & flags)
y = DisplayHeight(dpy, DefaultScreen(dpy)) + y
- sizehints.height;
sizehints.flags |= USPosition;
sizehints.y = y;
}
}
swa.border_pixel = 0;
swa.background_pixel = white.pixel;
swa.colormap = cmap;
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
| KeyReleaseMask;
window = XCreateWindow(dpy, RootWindow(dpy, vi->screen),
sizehints.x, sizehints.y,
sizehints.width, sizehints.height,
0, vi->depth, InputOutput, vi->visual,
CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
&swa);
XSetStandardProperties(dpy, window, "pixtest", "pixtest", None,
argv, argc, &sizehints);
XSetWMColormapWindows(dpy, window, &window, 1);
XMapWindow(dpy, window);
XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
if (!glXMakeCurrent(dpy, window, cx)) {
fprintf(stderr, "Can't make window current to context\n");
return -1;
}
Init();
needDisplay = GL_TRUE;
for (;;) {
do {
XNextEvent(dpy, &event);
switch (event.type) {
case Expose:
needDisplay = GL_TRUE;
break;
case ConfigureNotify:
windowWidth = event.xconfigure.width;
windowHeight = event.xconfigure.height;
glViewport(0, 0, windowWidth, windowHeight);
needDisplay = GL_TRUE;
break;
case KeyPress:
{
char buf[100];
int rv;
KeySym ks;
GLboolean setNeed = GL_TRUE;
rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
switch (ks) {
case XK_Escape:
exit(0);
case XK_I:
case XK_i:
setNeed = GL_FALSE;
glXSwapBuffers(dpy, window);
break;
case XK_x:
i = zoomx / 0.125;
i++;
zoomx = i / 8.0;
break;
case XK_X:
i = zoomx / 0.125;
i--;
zoomx = i / 8.0;
break;
case XK_y:
i = zoomy / 0.125;
i++;
zoomy = i / 8.0;
break;
case XK_Y:
i = zoomy / 0.125;
i--;
zoomy = i / 8.0;
break;
case XK_Left:
centerx -= 9;
break;
case XK_Right:
centerx += 9;
break;
case XK_Up:
centery += 9;
break;
case XK_Down:
centery -= 9;
break;
case XK_B:
case XK_b:
biasing = 1-biasing;
if (biasing) {
printf("Biasing on\n");
} else {
printf("Biasing off\n");
}
break;
case XK_M:
case XK_m:
mapping = 1-mapping;
if (mapping) {
printf("Mapping on\n");
} else {
printf("Mapping off\n");
}
break;
case XK_S:
case XK_s:
scissor = 1-scissor;
if (scissor) {
printf("Scissoring on\n");
} else {
printf("Scissoring off\n");
}
break;
case XK_R:
case XK_r:
changeTest();
break;
case XK_T:
case XK_t:
changeType();
break;
case XK_F:
case XK_f:
changeFormat();
break;
case XK_A:
case XK_a:
alphatst = 1-alphatst;
if (alphatst) {
printf("Alpha test on\n");
} else {
printf("Alpha test off\n");
}
break;
case XK_E:
case XK_e:
swapBytes = 1-swapBytes;
if (swapBytes) {
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
glPixelStorei(GL_PACK_SWAP_BYTES, GL_TRUE);
glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
} else {
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_PACK_LSB_FIRST, GL_TRUE);
}
makeNewImage();
break;
case XK_W:
case XK_w:
alignShift = (alignShift+1)&3;
makeNewImage();
break;
case XK_D:
case XK_d:
dlisted = 1-dlisted;
if (dlisted) {
printf("Display listing\n");
} else {
printf("Immediate mode\n");
}
break;
default:
setNeed = GL_FALSE;
break;
}
if (setNeed) needDisplay = GL_TRUE;
}
break;
}
} while (XPending(dpy) != 0);
if (needDisplay) {
needDisplay = GL_FALSE;
Redraw();
}
}
}